<?php

class Admin_Index_Controller extends Base_Controller {

  public function action_index()
  {
    $k = Input::get('k',null);  //关键字
    $a = Input::get('a',null);  //地区查询
    $p = Input::get('pro',null);  //地区查询
    $c = Input::get('cit',null);  //地区查询
    $w = Input::get('are',null);  //地区查询
    $data['shop_management.shopname'] = array('like','%'.$k.'%');
    $data['shop_management.shoper'] = array('like','%'.$k.'%');
    $data['shop_management.shoperphone'] = array('like','%'.$k.'%');
    $data['shop_management.mark'] = array('like','%'.$k.'%');
    $area['shop_management.province'] = array('like','%'.$p.'%');
    $area['shop_management.city'] = array('like','%'.$c.'%');
    $area['shop_management.county'] = array('like','%'.$w.'%');
    $stores = DB::table('shop_management')
            ->where('dele','=',1)
            ->where(function($query) use($data) {
            foreach($data as $column => $val)
            {
              $query->or_where($column, $val[0], $val[1]);
            }
            })
            ->where(function($query) use($area) {
            foreach($area as $column => $val)
            {
              $query->where($column, $val[0], $val[1]);
            }
            })->paginate(10);
    return View::make('admin/index',array('stores'=>$stores,'k'=>$k,'a'=>$a,'p'=>$p,'c'=>$c,'w'=>$w));
    
  }


  public function action_addshop()
  {
    return View::make('admin/addshop');
  }



  public function action_doaddshop()
  {
    $mark = Input::get('mark');
    $shopname = Input::get('shopname'); 
    $shoper = Input::get('shoper');
    $shoperphone = Input::get('shoperphone');
    $address = Input::get('address');
    $shophours = Input::get('shophours');
    $province = Input::get('pro');
    $city = Input::get('cit');
    $county = Input::get('are');
    $str = DB::table('shop_management')->insert(array('shopname'=>$shopname,'mark'=>$mark,'shoper'=>$shoper,'shoperphone'=>$shoperphone,'address'=>$address,'shophours'=>$shophours,'province'=>$province,'city'=>$city,'county'=>$county));
    if($str){
        return View::make('jump/success',array('msg'=>'添加成功,','time'=>1));
    }else{
        return View::make('jump/success',array('msg'=>'添加失败','time'=>2));
    }
  }


  public function action_dele()
  {
    $id = Input::get('id');
    $dele = 0;
    $str = DB::table('shop_management')->where('id','=',$id)->update(array('dele'=>$dele));
    if($str){
        $data = array(
            'code'=>'200',
            'msg'=>'删除成功'
            );
    }else{
        $data = array(
            'code'=>'400',
            'msg'=>'删除失败'
            );
    }
    return json_encode($data);
    }


  public function action_change()
  {
    $id = Input::get('id');
    $str = DB::table('shop_management')->where('id','=',$id)->first();
    return View::make('admin/shopchange',array('str'=>$str));
  }

  public function action_dochange()
  {
    $id = Input::get('thisid');
    $mark = Input::get('mark');
    $shopname = Input::get('shopname');
    $shoper = Input::get('shoper');
    $shoperphone = Input::get('shoperphone');
    $province = Input::get('pro');
    $city = Input::get('cit');
    $county = Input::get('are');
    $address = Input::get('address');
    $shophours = Input::get('shophours');
    $str = DB::table('shop_management')->where('id','=',$id)->update(array('mark'=>$mark,'shopname'=>$shopname,'shoper'=>$shoper,'shoperphone'=>$shoperphone,'address'=>$address,'province'=>$province,'city'=>$city,'county'=>$county,'shophours'=>$shophours));
    if($str){
        return View::make('jump/jumpto',array('msg'=>'门店信息修改成功','url'=>'index','time'=>2));
    }else{
        return View::make('jump/error',array('msg'=>'修改失败,不开心','time'=>5));
    }
  }


 


}